using System;
using System.Collections.Generic;
using System.Data;

namespace Glpt.Data
{
    /// <summary>
    /// 基础数据接口类
    /// </summary>
    public class BaseInterFace : AbstractInterFace
    {
        /// <summary>
        /// 特殊属性列的列名开头标志
        /// </summary>
        public const String ATTR_Column = "ATTR_";

        /// <summary>
        /// 特殊值列的列名开头标志
        /// </summary>
        public const String VALU_Column = "VALU_";

        /// <summary>
        /// 内部的数据库项目
        /// </summary>
        public static DbItemCollection DbList = new DbItemCollection();

        /// <summary>
        /// 链接的数据库项目
        /// </summary>
        public static Dictionary<DbItem, DbItemCollection> DbListLink = new Dictionary<DbItem, DbItemCollection>();

        /// <summary>
        /// 集群的数据库项目
        /// </summary>
        public static Dictionary<DbItem, DbItemCollection> DbListCluster = new Dictionary<DbItem, DbItemCollection>();

        /// <summary>
        /// 全部的用户列表
        /// </summary>
        public static UserInfoCollection UserList = new UserInfoCollection();

        /// <summary>
        /// 当前系统登录信息
        /// </summary>
        public static UserInfo LoginUserInfo = null;

        /// <summary>
        /// 当前系统登录的机器名
        /// </summary>
        public static string LoginPcName = "";

        /// <summary>
        /// 当前系统登录的IP
        /// </summary>
        public static string LoginIP = "";

        /// <summary>
        /// 当前系统登录的MAC
        /// </summary>
        public static string LoginMAC = "";

        /// <summary>
        /// 登录流水号
        /// </summary>
        public static string LoginID = "";

        /// <summary>
        /// 获取当前系统登录的用户名
        /// </summary>
        /// <returns>登录的用户名</returns>
        public static string GetLoginName()
        {
            if (LoginUserInfo == null)
                throw new Exception("您尚未登陆，请先登陆系统再操作！");
            return LoginUserInfo.UserName;
        }

        private AbstractInterFace aiface = null;
        private DataBaseType dbtype = DataBaseType.SQLServer;
        //启用WEB代理
        public static bool UseWebProxy = false;

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public BaseInterFace()
        {
            Init(DbList.GetByNo(0));
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="dbno">数据库项目编号</param>
        public BaseInterFace(int dbno)
        {
            Init(DbList.GetByNo(dbno));
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="dbitem">数据库项目</param>
        public BaseInterFace(DbItem dbitem)
        {
            Init(dbitem);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dbitem">数据库项目</param>
        private void Init(DbItem dbitem)
        {
            if (dbitem == null) throw new Exception("指定的数据库不能为空");
            bool isUseWebProxy = (dbitem.PROXY_DB_NO == 9999);//强制走WEB代理
            if (ParamSet.EnableProxy || UseWebProxy && isUseWebProxy)
                aiface = new WebInterFace(dbitem);
            else
            {
                if (dbitem.UseProxy && !isUseWebProxy)
                {
                    real_db = dbitem;
                    if (real_db.PROXY_DB_NO >= 0)
                        dbitem = DbList.GetByNo(real_db.PROXY_DB_NO);
                    else
                        dbitem = new BaseInterFace().GetDbItemOut(0 - real_db.PROXY_DB_NO);
                    if (dbitem == null) throw new Exception("没有找到指定的代理数据库");
                }
                if (dbitem.DB_TYPE.Equals(DataBaseType.SQLServer))
                    aiface = new SqlInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.Oracle))
                    aiface = new OracleInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.DB2))
                    aiface = new DB2InterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.MySQL))
                    aiface = new MySqlInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.OceanBase) || dbitem.DB_TYPE.Equals(DataBaseType.GoldenDB))
                    aiface = new OceanBaseInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.SybaseIQ))
                    aiface = new SAInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.PostgreSQL))
                    aiface = new NpgsqlInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.GaussDB))
                    aiface = new GaussInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.SQLite))
                    aiface = new SQLiteInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.DaMeng))
                    aiface = new DmInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.Kingbase))
                    aiface = new KingbaseInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.ClickHouse))
                    aiface = new ClickHouseInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.SybaseASE))
                    aiface = new ASEInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.Teradata))
                    aiface = new TdInterFace(dbitem);
                else if (dbitem.DB_TYPE.Equals(DataBaseType.ODBC))
                    aiface = new OdbcInterFace(dbitem);
                else
                    aiface = new OledbInterFace(dbitem);
                aiface.real_db = real_db;
            }
            dbtype = dbitem.DB_TYPE;
        }

        /// <summary>
        /// 等待命令执行的时间（以秒为单位）。0表示没限制。
        /// </summary>
        public new int CommandTimeout
        {
            get
            {
                return aiface.CommandTimeout;
            }
            set
            {
                aiface.CommandTimeout = value;
            }
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DataBaseType DB_TYPE
        {
            get
            {
                if (real_db != null)
                    return real_db.DB_TYPE;
                else
                    return dbtype;
            }
        }
        /// <summary>
        /// 描述列元数据的DataTable
        /// </summary>
        public override DataTable SchemaTable
        {
            get
            {
                return aiface.SchemaTable;
            }
        }

        /// <summary>
        /// 参数字符标志
        /// </summary>
        public override string ParamChar
        {
            get
            {
                return aiface.ParamChar;
            }
        }

        /// <summary>
        /// 函数字符标志
        /// </summary>
        public override string FunctionChar
        {
            get
            {
                return aiface.FunctionChar;
            }
        }

        /// <summary>
        /// 字符串连接标志
        /// </summary>
        public override string ConcatChar
        {
            get
            {
                return aiface.ConcatChar;
            }
        }

        /// <summary>
        /// 字段左字符
        /// </summary>
        public override string ColumnLeftChar
        {
            get
            {
                if (real_db != null)
                    return "";
                else
                    return aiface.ColumnLeftChar;
            }
        }

        /// <summary>
        /// 字段右字符
        /// </summary>
        public override string ColumnRightChar
        {
            get
            {
                if (real_db != null)
                    return "";
                else
                    return aiface.ColumnRightChar;
            }
        }

        /// <summary>
        /// 是否有数据库事务
        /// </summary>
        /// <returns>是否</returns>
        public override bool HasTransaction()
        {
            return aiface.HasTransaction();
        }

        /// <summary>
        /// 开始数据库事务
        /// </summary>
        /// <returns>是否成功</returns>
        public override bool BeginTransaction()
        {
            return aiface.BeginTransaction();
        }

        /// <summary>
        /// 提交数据库事务
        /// </summary>
        public override void Commit()
        {
            aiface.Commit();
        }

        /// <summary>
        /// 从挂起状态回滚事务
        /// </summary>
        public override void Rollback()
        {
            aiface.Rollback();
        }

        /// <summary>
        /// 执行SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果集</returns>
        public override DataTable ExecSelect(string sql)
        {
            return aiface.ExecSelect(sql);
        }

        /// <summary>
        /// 执行SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <returns>结果集</returns>
        public override DataTable ExecSelect(string sql, DbParamCollection sql_params)
        {
            return aiface.ExecSelect(sql, sql_params);
        }

        /// <summary>
        /// 执行SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <param name="is_pro">是否是存储过程</param>
        /// <returns>结果集</returns>
        public override DataTable ExecSelect(string sql, DbParamCollection sql_params, bool is_pro)
        {
            return aiface.ExecSelect(sql, sql_params, is_pro);
        }

        /// <summary>
        /// 执行SQL查询
        /// </summary>
        /// <param name="searchHostObj">发起执行的主体对象</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <param name="is_pro">是否是存储过程</param>
        /// <param name="pageIndex">页码，从0开始，-1表示全部</param>
        /// <returns>结果集</returns>
        public override DataTable ExecSelect(object searchHostObj, string sql, DbParamCollection sql_params, bool is_pro, int pageIndex)
        {
            return aiface.ExecSelect(searchHostObj, sql, sql_params, is_pro, pageIndex);
        }

        /// <summary>
        /// 批量加载数据
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="map_mode">字段映射 0按名称 1按顺序</param>
        /// <param name="reader">数据流</param>
        /// <param name="batchsize">每一批次中的行数</param>
        public override void BulkCopy(string tablename, string map_mode, IDataReader reader, int batchsize)
        {
            aiface.BulkCopy(tablename, map_mode, reader, batchsize);
        }

        /// <summary>
        /// 批量加载数据
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="map_mode">字段映射 0按名称 1按顺序</param>
        /// <param name="table">数据表</param>
        /// <param name="batchsize">每一批次中的行数</param>
        public override void BulkCopy(string tablename, string map_mode, DataTable table, int batchsize)
        {
            aiface.BulkCopy(tablename, map_mode, table, batchsize);
        }

        /// <summary>
        /// 批量加载数据
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="map_mode">字段映射 0按名称 1按顺序</param>
        /// <param name="rows">数据行</param>
        /// <param name="batchsize">每一批次中的行数</param>
        public override void BulkCopy(string tablename, string map_mode, DataRow[] rows, int batchsize)
        {
            aiface.BulkCopy(tablename, map_mode, rows, batchsize);
        }

        /// <summary>
        /// 获取数据流
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>数据流</returns>
        public override IDataReader ExecReader(string sql)
        {
            return aiface.ExecReader(sql);
        }

        /// <summary>
        /// 获取数据流
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <returns>数据流</returns>
        public override IDataReader ExecReader(string sql, DbParamCollection sql_params)
        {
            return aiface.ExecReader(sql, sql_params);
        }

        /// <summary>
        /// 获取数据流
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <param name="is_pro">是否是存储过程</param>
        /// <returns>数据流</returns>
        public override IDataReader ExecReader(string sql, DbParamCollection sql_params, bool is_pro)
        {
            return aiface.ExecReader(sql, sql_params, is_pro);
        }

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns>结果集</returns>
        public override int ExecUpdate(string sql)
        {
            return aiface.ExecUpdate(sql);
        }

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <returns>结果集</returns>
        public override int ExecUpdate(string sql, DbParamCollection sql_params)
        {
            return aiface.ExecUpdate(sql, sql_params);
        }

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <param name="is_pro">是否是存储过程</param>
        /// <returns>结果集</returns>
        public override int ExecUpdate(string sql, DbParamCollection sql_params, bool is_pro)
        {
            return aiface.ExecUpdate(sql, sql_params, is_pro);
        }

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="searchHostObj">发起执行的主体对象</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="sql_params">SQL参数列表</param>
        /// <param name="is_pro">是否是存储过程</param>
        /// <returns>结果集</returns>
        public override int ExecUpdate(object searchHostObj, string sql, DbParamCollection sql_params, bool is_pro)
        {
            return aiface.ExecUpdate(searchHostObj, sql, sql_params, is_pro);
        }

        /// <summary>
        /// 获取功能定义信息
        /// </summary>
        /// <param name="function_id">功能号</param>
        /// <param name="username">用户名</param>
        /// <param name="isperson">是否要取个性化信息</param>
        /// <returns>功能定义信息</returns>
        public DataSet GetResultFunctionInfo(string function_id, string username, bool isperson)
        {
            DataSet dset = new DataSet();
            DataTable dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_FUNCTION WHERE FUNCTION_ID=" + function_id);
            dt.TableName = "TB_RESULT_FUNCTION";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_INPUT_CONDITION WHERE FUNCTION_ID=" + function_id + " ORDER BY INPUT_SEQ");
            dt.TableName = "TB_RESULT_INPUT_CONDITION";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_OUTPUT_CONTROL WHERE FUNCTION_ID=" + function_id + " ORDER BY FIELD_SEQ");
            dt.TableName = "TB_RESULT_OUTPUT_CONTROL";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_OUTPUT_STYLE WHERE FUNCTION_ID=" + function_id + " ORDER BY STYLE_SEQ");
            dt.TableName = "TB_RESULT_OUTPUT_STYLE";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_CHART WHERE FUNCTION_ID=" + function_id + " ORDER BY CHART_SEQ");
            dt.TableName = "TB_RESULT_CHART";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_BUTTON WHERE FUNCTION_ID=" + function_id + " ORDER BY BUTTON_LEVEL");
            dt.TableName = "TB_RESULT_BUTTON";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_RESULT_BUTTON_PARAMS WHERE FUNCTION_ID=" + function_id + " ORDER BY BUTTON_LEVEL,PARAM_ID");
            dt.TableName = "TB_RESULT_BUTTON_PARAMS";
            dset.Tables.Add(dt);
            if (isperson)
            {
                dt = aiface.ExecSelect("SELECT * FROM TB_USER_OUTPUT_CONTROL WHERE USERNAME='" + username.Replace("'", "''") + "' AND FUNCTION_ID=" + function_id + " ORDER BY FIELD_SEQ");
                dt.TableName = "TB_USER_OUTPUT_CONTROL";
                dset.Tables.Add(dt);
                dt = aiface.ExecSelect("SELECT * FROM TB_USER_CHART WHERE USERNAME='" + username.Replace("'", "''") + "' AND FUNCTION_ID=" + function_id + " ORDER BY CHART_SEQ");
                dt.TableName = "TB_USER_CHART";
                dset.Tables.Add(dt);
            }
            return dset;
        }

        /// <summary>
        /// 获取表配置定义信息
        /// </summary>
        /// <param name="table_id">表编号</param>
        /// <returns>表配置定义信息</returns>
        public DataSet GetTableInfo(string table_id)
        {
            DataSet dset = new DataSet();
            DataTable dt = aiface.ExecSelect("SELECT * FROM TB_TABLE_CONFIG WHERE TABLE_ID=" + table_id);
            dt.TableName = "TB_TABLE_CONFIG";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_TABLE_FIELD WHERE TABLE_ID=" + table_id + " ORDER BY FIELD_ID");
            dt.TableName = "TB_TABLE_FIELD";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_TABLE_PARAMS WHERE TABLE_ID=" + table_id + " ORDER BY OP_TYPE,PROC_PARAMS_SEQ");
            dt.TableName = "TB_TABLE_PARAMS";
            dset.Tables.Add(dt);
            return dset;
        }

        /// <summary>
        /// 保存功能信息
        /// </summary>
        /// <param name="dset">功能信息数据</param>
        /// <param name="op_type">操作类别：0添加，1修改，2删除</param>
        public void SaveFunctionInfo(DataSet dset, int op_type)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string function_id = dset.Tables["TB_RESULT_FUNCTION"].Rows[0]["FUNCTION_ID"].ToString();
                DataTable dtcount = aiface.ExecSelect("SELECT COUNT(*) FROM TB_RESULT_FUNCTION WHERE FUNCTION_ID=" + function_id);
                if (op_type == 0 && int.Parse(dtcount.Rows[0][0].ToString()) > 0)
                    throw new Exception("该功能号已经存在！");
                for (int i = 0; i < dset.Tables.Count; i++)
                {
                    DataTable dt = dset.Tables[i];
                    if (dt.TableName.StartsWith("TB_RESULT_"))
                    {
                        aiface.ExecUpdate("DELETE FROM " + dt.TableName + " WHERE FUNCTION_ID=" + function_id);
                        if (op_type < 2)
                        {
                            string insertsql = "";
                            string valuessql = "";
                            DbParamCollection sql_params = new DbParamCollection();
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                insertsql += "," + dt.Columns[j].ColumnName;
                                valuessql += "," + aiface.ParamChar + dt.Columns[j].ColumnName;
                                sql_params.Add(new DbParam(dt.Columns[j].ColumnName, dt.Columns[j].DataType.IsValueType ? DbParamType.Double : DbParamType.VarChar));
                            }
                            string sql = "insert into " + dt.TableName + "(" + insertsql.Substring(1) + ") values(" + valuessql.Substring(1) + ")";
                            for (int k = 0; k < dt.Rows.Count; k++)
                            {
                                for (int j = 0; j < dt.Columns.Count; j++)
                                {
                                    sql_params[j].Value = dt.Rows[k][j];
                                }
                                aiface.ExecUpdate(sql, sql_params);
                            }
                        }
                    }
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 保存自定义功能信息
        /// </summary>
        /// <param name="dset">功能信息数据</param>
        /// <param name="username">用户名</param>
        /// <param name="op_type">操作类别：0添加，1修改，2删除</param>
        public void SaveUserSet(DataSet dset, string username, int op_type)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string function_id = dset.Tables["TB_RESULT_FUNCTION"].Rows[0]["FUNCTION_ID"].ToString();
                for (int i = 0; i < dset.Tables.Count; i++)
                {
                    DataTable dt = dset.Tables[i];
                    if (dt.TableName.Equals("TB_RESULT_CHART") || dt.TableName.Equals("TB_RESULT_OUTPUT_CONTROL"))
                    {
                        string tabname = dt.TableName.Replace("TB_RESULT_", "TB_USER_");

                        aiface.ExecUpdate("DELETE FROM " + tabname + " WHERE " + (username.Length > 0 ? "USERNAME='" + username.Replace("'", "''") + "' AND " : "") + "FUNCTION_ID=" + function_id);
                        if (op_type < 2)
                        {
                            string insertsql = "USERNAME";
                            string valuessql = aiface.ParamChar + "USERNAME";
                            DbParamCollection sql_params = new DbParamCollection();
                            sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                insertsql += "," + dt.Columns[j].ColumnName;
                                valuessql += "," + aiface.ParamChar + dt.Columns[j].ColumnName;
                                sql_params.Add(new DbParam(dt.Columns[j].ColumnName, dt.Columns[j].DataType.IsValueType ? DbParamType.Double : DbParamType.VarChar));
                            }
                            string sql = "insert into " + tabname + "(" + insertsql + ") values(" + valuessql + ")";
                            for (int k = 0; k < dt.Rows.Count; k++)
                            {
                                for (int j = 0; j < dt.Columns.Count; j++)
                                {
                                    sql_params[j + 1].Value = dt.Rows[k][j];
                                }
                                aiface.ExecUpdate(sql, sql_params);
                            }
                        }
                    }
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 保表存配置信息
        /// </summary>
        /// <param name="dset">配置信息数据</param>
        /// <param name="op_type">操作类别：0添加，1修改，2删除</param>
        public void SaveTableInfo(DataSet dset, int op_type)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string table_id = dset.Tables["TB_TABLE_CONFIG"].Rows[0]["TABLE_ID"].ToString();
                DataTable dtcount = aiface.ExecSelect("SELECT COUNT(*) FROM TB_TABLE_CONFIG WHERE TABLE_ID=" + table_id);
                if (op_type == 0 && int.Parse(dtcount.Rows[0][0].ToString()) > 0)
                    throw new Exception("该表编号已经存在！");
                for (int i = 0; i < dset.Tables.Count; i++)
                {
                    DataTable dt = dset.Tables[i];
                    aiface.ExecUpdate("DELETE FROM " + dt.TableName + " WHERE TABLE_ID=" + table_id);
                    if (op_type < 2)
                    {
                        string insertsql = "";
                        string valuessql = "";
                        DbParamCollection sql_params = new DbParamCollection();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            insertsql += "," + dt.Columns[j].ColumnName;
                            valuessql += "," + aiface.ParamChar + dt.Columns[j].ColumnName;
                            sql_params.Add(new DbParam(dt.Columns[j].ColumnName, dt.Columns[j].DataType.IsValueType ? DbParamType.Double : DbParamType.VarChar));
                        }
                        string sql = "insert into " + dt.TableName + "(" + insertsql.Substring(1) + ") values(" + valuessql.Substring(1) + ")";
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                sql_params[j].Value = dt.Rows[k][j];
                            }
                            aiface.ExecUpdate(sql, sql_params);
                        }
                    }
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 保存工作流配置信息
        /// </summary>
        /// <param name="dset">配置信息数据</param>
        /// <param name="op_type">操作类别：0添加，1修改，2删除</param>
        public void SaveWorkflowInfo(DataSet dset, int op_type)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string workflow_id = dset.Tables["TB_WORKFLOW"].Rows[0]["WORKFLOW_ID"].ToString();
                DataTable dtcount = aiface.ExecSelect("SELECT COUNT(*) FROM TB_WORKFLOW WHERE WORKFLOW_ID=" + workflow_id);
                if (op_type == 0 && int.Parse(dtcount.Rows[0][0].ToString()) > 0)
                    throw new Exception("该流程编号已经存在！");
                for (int i = 0; i < dset.Tables.Count; i++)
                {
                    DataTable dt = dset.Tables[i];
                    aiface.ExecUpdate("DELETE FROM " + dt.TableName + " WHERE WORKFLOW_ID=" + workflow_id);
                    if (op_type < 2)
                    {
                        string insertsql = "";
                        string valuessql = "";
                        DbParamCollection sql_params = new DbParamCollection();
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            insertsql += "," + dt.Columns[j].ColumnName;
                            valuessql += "," + aiface.ParamChar + dt.Columns[j].ColumnName;
                            sql_params.Add(new DbParam(dt.Columns[j].ColumnName, dt.Columns[j].DataType.IsValueType ? DbParamType.Double : DbParamType.VarChar));
                        }
                        string sql = "insert into " + dt.TableName + "(" + insertsql.Substring(1) + ") values(" + valuessql.Substring(1) + ")";
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                sql_params[j].Value = dt.Rows[k][j];
                            }
                            aiface.ExecUpdate(sql, sql_params);
                        }
                    }
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 保存系统日志
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="op_message">日志消息</param>
        public void SaveSysLog(string username, string op_message)
        {
            try
            {
                if (username.Length == 0) username = GetLoginName();
                string sql = "INSERT INTO TB_LOG_SYS(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "LOG_ID," : "") + "USERNAME,OP_MESSAGE,PC_NAME,IP,MAC) VALUES(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "SEQ_LOG_ID.nextval," : "") + aiface.ParamChar + "USERNAME," + aiface.ParamChar + "OP_MESSAGE," + aiface.ParamChar + "PC_NAME," + aiface.ParamChar + "IP," + aiface.ParamChar + "MAC)";
                DbParamCollection sql_params = new DbParamCollection();
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("OP_MESSAGE", op_message, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("PC_NAME", LoginPcName, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("IP", LoginIP, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("MAC", LoginMAC, DbParamType.VarChar, false));
                aiface.ExecUpdate(sql, sql_params);
            }
            catch { }
        }

        /// <summary>
        /// 保存查询日志
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="function_id">功能号</param>
        /// <param name="params_name">参数名列表</param>
        /// <param name="params_value">参数值列表</param>
        /// <param name="execute_time">执行时间（毫秒）</param>
        public void SaveFunctionLog(string username, string function_id, string params_name, string params_value, long execute_time)
        {
            try
            {
#if (!DEBUG)
                if (username.Length == 0) username = GetLoginName();
                string sql = "INSERT INTO TB_LOG_FUNCTION(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "LOG_ID," : "") + "USERNAME,FUNCTION_ID,PARAMS_NAME,PARAMS_VALUE,EXECUTED_TIME,PC_NAME,IP,MAC) VALUES(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "SEQ_LOG_ID.nextval," : "") + aiface.ParamChar + "USERNAME," + aiface.ParamChar + "FUNCTION_ID," + aiface.ParamChar + "PARAMS_NAME," + aiface.ParamChar + "PARAMS_VALUE," + aiface.ParamChar + "EXECUTED_TIME," + aiface.ParamChar + "PC_NAME," + aiface.ParamChar + "IP," + aiface.ParamChar + "MAC)";
                DbParamCollection sql_params = new DbParamCollection();
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("FUNCTION_ID", function_id, DbParamType.Int, false));
                sql_params.Add(new DbParam("PARAMS_NAME", params_name, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("PARAMS_VALUE", params_value, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("EXECUTED_TIME", execute_time, DbParamType.Int, false));
                sql_params.Add(new DbParam("PC_NAME", LoginPcName, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("IP", LoginIP, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("MAC", LoginMAC, DbParamType.VarChar, false));
                aiface.ExecUpdate(sql, sql_params);
#endif//DEBUG
            }
            catch { }
        }

        /// <summary>
        /// 保存更新日志
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="table_id">表编号</param>
        /// <param name="op_type">操作类别：0添加，1修改，2删除</param>
        /// <param name="inputnames">输入参数名列表</param>
        /// <param name="inputvalues">输入参数值列表</param>
        /// <param name="keynames">主键参数名列表</param>
        /// <param name="keyvalues">主键参数值列表</param>
        public void SaveTableLog(string username, string table_id, string op_type, string inputnames, string inputvalues, string keynames, string keyvalues)
        {
            try
            {
#if (!DEBUG)
                if (username.Length == 0) username = GetLoginName();
                string sql = "INSERT INTO TB_LOG_TABLE(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "LOG_ID," : "") + "USERNAME,TABLE_ID,OP_TYPE,INPUTNAMES,INPUTVALUES,KEYNAMES,KEYVALUES,PC_NAME,IP,MAC) VALUES(" + (this.DB_TYPE.Equals(DataBaseType.Oracle) ? "SEQ_LOG_ID.nextval," : "") + aiface.ParamChar + "USERNAME," + aiface.ParamChar + "TABLE_ID," + aiface.ParamChar + "OP_TYPE," + aiface.ParamChar + "INPUTNAMES," + aiface.ParamChar + "INPUTVALUES," + aiface.ParamChar + "KEYNAMES," + aiface.ParamChar + "KEYVALUES," + aiface.ParamChar + "PC_NAME," + aiface.ParamChar + "IP," + aiface.ParamChar + "MAC)";
                DbParamCollection sql_params = new DbParamCollection();
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("TABLE_ID", table_id, DbParamType.Int, false));
                sql_params.Add(new DbParam("OP_TYPE", op_type, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("INPUTNAMES", inputnames, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("INPUTVALUES", inputvalues, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("KEYNAMES", keynames, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("KEYVALUES", keyvalues, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("PC_NAME", LoginPcName, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("IP", LoginIP, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("MAC", LoginMAC, DbParamType.VarChar, false));
                aiface.ExecUpdate(sql, sql_params);
#endif//DEBUG
            }
            catch { }
        }

        /// <summary>
        /// 系统登录
        /// </summary>
        /// <param name="searchHostObj">发起执行的主体对象</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="ip">IP地址</param>
        /// <param name="mac_list">MAC地址</param>
        /// <param name="computer_name">计算机名称</param>
        /// <param name="version">版本号</param>
        /// <returns>登录用户的信息</returns>
        public UserInfo Login(object searchHostObj, string username, string password, string ip, string[] mac_list, string computer_name, string version)
        {
            string maclist = "";
            foreach (string mac in mac_list)
            {
                maclist += ";" + mac.Replace(":", "");
            }
            if (maclist.Length > 0) maclist = maclist.Substring(1);
            LoginPcName = computer_name;
            LoginIP = ip;
            LoginMAC = maclist;
            UserInfo uinfo = null;
            string sql = "PR_LOGIN";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("PASSWORD", password, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("VERSION", version, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("PC_NAME", computer_name, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("IP", ip, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("MAC", maclist, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("SERIAL", "", DbParamType.VarChar, false));
            DataTable dt = aiface.ExecSelect(searchHostObj, sql, sql_params, true, -1);
            if (dt.Rows.Count == 0)
                return uinfo;
            ServerTime.ServerDateTime = (DateTime)dt.Rows[0]["LOGIN_TIME"];
            LoginID = dt.Rows[0]["LOGIN_ID"].ToString();
            uinfo = new UserInfo(dt.Rows[0]);
            return uinfo;
        }

        /// <summary>
        /// 检测链接登录用户权限
        /// </summary>
        /// <param name="sys">系统来源</param>
        /// <param name="username">用户名</param>
        /// <param name="fun_type">功能类型 0菜单 1功能 2表</param>
        /// <param name="fun_id">菜单编号或功能号</param>
        public bool CheckUserLink(string sys, string username, string fun_type, string fun_id)
        {
            bool ret = false;
            try
            {
                string sql = "PR_LOGIN_LINK";
                DbParamCollection sql_params = new DbParamCollection();
                sql_params.Add(new DbParam("SOURCE_SYS", sys, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("FUN_TYPE", fun_type, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("FUN_ID", fun_id, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("VISIT_TIME", ServerTime.ServerDateTime, DbParamType.DateTime, false));
                sql_params.Add(new DbParam("IP", LoginIP, DbParamType.VarChar, false));
                DataTable dt = aiface.ExecSelect(null, sql, sql_params, true, -1);
                if (dt.Rows.Count > 0)
                {
                    ret = true;
                }
            }
            catch { }
            return ret;
        }

        /// <summary>
        /// 系统登出
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="ip">IP地址</param>
        public void Logout(string username, string ip)
        {
            SaveSysLog(username, "退出系统成功！");
        }

        /// <summary>
        /// 设置用户密码
        /// </summary>
        /// <param name="username">用户帐号</param>
        /// <param name="password">密码</param>
        public void SetUserPassWord(string username, string password)
        {
            string sql = "UPDATE TB_USER SET PASSWORD=" + aiface.ParamChar + "PASSWORD WHERE USERNAME=" + aiface.ParamChar + "USERNAME";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("PASSWORD", password, 0, false));
            sql_params.Add(new DbParam("USERNAME", username, 0, false));
            aiface.ExecUpdate(sql, sql_params);
        }
        /// <summary>
        /// 获取交易日
        /// </summary>
        /// <returns>交易日列表</returns>
        public DataTable GetTradeDays()
        {
            string sql = "SELECT * FROM VW_TRADE_DAYS";
            return aiface.ExecSelect(sql);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="menu_level">菜单级别</param>
        /// <param name="subsystem">子系统编号</param>
        /// <param name="username">用户帐号</param>
        /// <returns>菜单列表</returns>
        public DataTable GetMenu(string menu_level, string subsystem, string username)
        {
            DbParamCollection sql_params = new DbParamCollection();
            string sql = "SELECT C.* FROM TB_MENU C";
            if (username.Length > 0)
            {
                sql = "SELECT C.*,B.OPERATE_RIGHT FROM TB_MENU C INNER JOIN VW_USER_MENU_RIGHT B ON C.MENU_LEVEL=B.MENU_LEVEL AND B.USERNAME=" + aiface.ParamChar + "USERNAME";
                sql_params.Add(new DbParam("USERNAME", username, 0, false));
            }
            if (menu_level.Length > 0)
            {
                sql += " WHERE C.MENU_LEVEL LIKE '" + menu_level + "%'";
            }
            if (subsystem.Length > 0)
            {
                if (menu_level.Length > 0)
                    sql += " AND";
                else
                    sql += " WHERE";
                sql += " COALESCE(C.SUB_SYSTEM,' ') IN (' ','" + subsystem + "')";
            }
            return aiface.ExecSelect(sql, sql_params);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns>用户列表</returns>
        public DataTable GetUserList()
        {
            string sql = "SELECT * FROM TB_USER";
            return aiface.ExecSelect(sql);
        }

        /// <summary>
        /// 获取数据库列表
        /// </summary>
        /// <param name="list_type">0简要列表 1详细列表 2链接列表</param>
        /// <returns>数据库列表</returns>
        public DataTable GetDbList(int list_type)
        {
            string sql = "PR_DATABASE";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("LIST_TYPE", list_type, DbParamType.Int, false));
            sql_params.Add(new DbParam("LOGIN_ID", LoginID, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("ZK_IP", ParamSet.GetParamData("ServerIP"), DbParamType.VarChar, false));
            return aiface.ExecSelect(null, sql, sql_params, true, -1);
        }
        /// <summary>
        /// 获取外部数据源
        /// </summary>
        /// <param name="db_no">编号</param>
        /// <returns>数据源</returns>
        public DbItem GetDbItemOut(int db_no)
        {
            string sql = "SELECT * FROM TB_EXCH_DATABASE WHERE DB_NO=@DB_NO";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("DB_NO", db_no, DbParamType.Int, false));
            DataTable dt = aiface.ExecSelect(sql, sql_params);
            if (dt.Rows.Count > 0)
                return new DbItem(dt.Rows[0]);
            else
                throw new Exception("没有找到指定的数据源");
        }

        /// <summary>
        /// 获取字典字典列表
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>字典字典列表</returns>
        public DataTable GetDict(string username)
        {
            string sql = "PR_DICT";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
            return aiface.ExecSelect(null, sql, sql_params, true, -1);
        }

        /// <summary>
        /// 获取加密算法列表
        /// </summary>
        /// <param name="decrypt_type">0加密 1解密</param>
        /// <returns>加密算法列表</returns>
        public DataTable GetCrypto(int decrypt_type)
        {
            string sql = "PR_DATACRYPTO";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("DECRYPT_TYPE", decrypt_type, DbParamType.Int, false));
            return aiface.ExecSelect(null, sql, sql_params, true, -1);
        }

        /// <summary>
        /// 获取预警级别列表
        /// </summary>
        /// <returns>预警级别列表</returns>
        public DataTable GetAlarmLevel()
        {
            string sql = "SELECT * FROM TB_ALARM_LEVEL";
            return aiface.ExecSelect(sql);
        }

        /// <summary>
        /// 保存结果数据
        /// </summary>
        /// <param name="dvresult">结果视图</param>
        /// <param name="tablename">表名</param>
        /// <param name="sql_where">删除条件</param>
        /// <param name="columnnames">字段名列表</param>
        /// <param name="isValueType">是否数值类型</param>
        /// <param name="database_obj_name">数据库对象名称</param>
        /// <param name="proc_params">参数列表</param>
        public void SaveResult(DataView dvresult, string tablename, string sql_where, string[] columnnames, bool[] isValueType, string database_obj_name, DbParamCollection proc_params)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string insertsql = "";
                string valuessql = "";
                DbParamCollection sql_params = new DbParamCollection();
                for (int j = 0; j < columnnames.Length; j++)
                {
                    if (columnnames[j] == null || columnnames[j].Length == 0) continue;
                    string pname = "P" + j.ToString();
                    insertsql += "," + aiface.ColumnLeftChar + columnnames[j].Trim() + aiface.ColumnRightChar;
                    valuessql += "," + aiface.ParamChar + pname;
                    DbParamType dtype = DbParamType.NVarChar;
                    if (!dvresult.Table.Columns.Contains(columnnames[j]))
                        throw new Exception(columnnames[j] + "列不存在！");
                    if (dvresult.Table.Columns[columnnames[j]].DataType.Equals(typeof(DateTime)))
                        dtype = DbParamType.DateTime;
                    else if (isValueType[j] && !dvresult.Table.Columns[columnnames[j]].DataType.IsValueType)
                        dtype = DbParamType.Double;
                    sql_params.Add(new DbParam(pname, dtype));
                }
                aiface.ExecUpdate("DELETE FROM " + tablename + (sql_where.Length > 0 ? " WHERE " + sql_where : ""));
                string sql = "insert into " + tablename + "(" + insertsql.Substring(1) + ") values(" + valuessql.Substring(1) + ")";
                for (int k = 0; k < dvresult.Count; k++)
                {
                    for (int j = 0; j < columnnames.Length; j++)
                    {
                        if (columnnames[j] == null || columnnames[j].Length == 0) continue;
                        string pname = "P" + j.ToString();
                        DbParam dbp = sql_params[pname];
                        if (dbp == null) continue;
                        if (isValueType[j] && !dvresult.Table.Columns[columnnames[j]].DataType.IsValueType)
                        {
                            string svalue = dvresult[k][columnnames[j]].ToString().Replace("%", "");
                            double dtmp = 0;
                            if (double.TryParse(svalue, out dtmp))
                                dbp.Value = dtmp;
                            else
                                dbp.Value = DBNull.Value;
                        }
                        else
                            dbp.Value = dvresult[k][columnnames[j]];

                    }
                    aiface.ExecUpdate(sql, sql_params);
                }
                if (database_obj_name.Length > 0)
                {
                    aiface.ExecUpdate(database_obj_name, proc_params, true);
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 保存修改的数据
        /// </summary>
        /// <param name="dt">修改的数据表</param>
        /// <param name="tablename">表名</param>
        /// <param name="keycolumn">主键字段列表</param>
        /// <param name="modifycolumn">修改字段列表</param>
        public void SaveResult(DataTable dt, string tablename, string[] keycolumn, string[] modifycolumn)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string updatesql = "";
                string wheresql = "";
                DbParamCollection sql_params = new DbParamCollection();
                for (int j = 0; j < modifycolumn.Length; j++)
                {
                    if (j > 0) updatesql += " , ";
                    updatesql += aiface.ColumnLeftChar + modifycolumn[j] + aiface.ColumnRightChar + "=" + aiface.ParamChar + modifycolumn[j];
                    sql_params.Add(new DbParam(modifycolumn[j], dt.Columns[modifycolumn[j]].DataType.IsValueType ? DbParamType.Double : DbParamType.NVarChar));
                }
                for (int j = 0; j < keycolumn.Length; j++)
                {
                    if (j > 0) wheresql += " and ";
                    wheresql += keycolumn[j] + "=" + aiface.ParamChar + keycolumn[j];
                    sql_params.Add(new DbParam(keycolumn[j], dt.Columns[keycolumn[j]].DataType.IsValueType ? DbParamType.Double : DbParamType.NVarChar));
                }
                string sql = "update " + tablename + " set " + updatesql + " where " + wheresql;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < modifycolumn.Length; j++)
                    {
                        sql_params[modifycolumn[j]].Value = dt.Rows[i][modifycolumn[j]];
                    }
                    for (int j = 0; j < keycolumn.Length; j++)
                    {
                        sql_params[keycolumn[j]].Value = dt.Rows[i][keycolumn[j]];
                    }
                    aiface.ExecUpdate(sql, sql_params);
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 通用查询记录
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="KeyInputParam">主键列表</param>
        public DataTable CommSelect(string tablename, DbParamCollection KeyInputParam)
        {
            string sql_where = "";
            for (int i = 0; i < KeyInputParam.Count; i++)
            {
                sql_where += " AND " + ColumnLeftChar + KeyInputParam[i].ParameterName + ColumnRightChar;
                KeyInputParam[i].ParameterName = "K" + i.ToString();
                sql_where += "=" + aiface.ParamChar + KeyInputParam[i].ParameterName;
            }
            if (sql_where.Length > 0) sql_where = " WHERE " + sql_where.Substring(" AND ".Length);
            sql_where = "SELECT * FROM " + tablename + sql_where;
            return aiface.ExecSelect(sql_where, KeyInputParam);
        }

        /// <summary>
        /// 通用插入记录
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="InputParam">输入条件列表</param>
        /// <param name="KeyInputParam">主键列表</param>
        public void CommInsert(string tablename, DbParamCollection InputParam, DbParamCollection KeyInputParam)
        {
            string sql = "";
            if (KeyInputParam.Count > 0)
            {
                string sql_where = "";
                for (int i = 0; i < KeyInputParam.Count; i++)
                {
                    sql_where += " AND " + ColumnLeftChar + KeyInputParam[i].ParameterName + ColumnRightChar;
                    sql_where += "=" + aiface.ParamChar + KeyInputParam[i].ParameterName;
                }
                if (sql_where.Length > 0) sql_where = " WHERE " + sql_where.Substring(" AND ".Length);
                sql = "SELECT COUNT(*) FROM " + tablename + sql_where;
                DataTable dt = aiface.ExecSelect(sql, KeyInputParam);
                if (int.Parse(dt.Rows[0][0].ToString()) > 0)
                    throw new Exception("该记录已经存在！");
            }
            string insertsql = "";
            string valuessql = "";
            for (int j = 0; j < InputParam.Count; j++)
            {
                insertsql += "," + ColumnLeftChar + InputParam[j].ParameterName + ColumnRightChar;
                InputParam[j].ParameterName = "P" + j.ToString();
                valuessql += "," + aiface.ParamChar + InputParam[j].ParameterName;
            }
            sql = "INSERT INTO " + tablename + "(" + insertsql.Substring(1) + ") VALUES(" + valuessql.Substring(1) + ")";
            aiface.ExecUpdate(sql, InputParam);
        }

        /// <summary>
        /// 通用更新记录
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="InputParam">输入条件列表</param>
        /// <param name="KeyInputParam">主键列表</param>
        public void CommUpdate(string tablename, DbParamCollection InputParam, DbParamCollection KeyInputParam)
        {
            string sql = "";
            string sql_where = "";
            for (int i = 0; i < KeyInputParam.Count; i++)
            {
                sql_where += " AND " + ColumnLeftChar + KeyInputParam[i].ParameterName + ColumnRightChar;
                KeyInputParam[i].ParameterName = "K" + i.ToString();
                sql_where += "=" + aiface.ParamChar + KeyInputParam[i].ParameterName;
            }
            if (sql_where.Length > 0) sql_where = " WHERE " + sql_where.Substring(" AND ".Length);
            sql = "SELECT COUNT(*) FROM " + tablename + sql_where;
            DataTable dt = aiface.ExecSelect(sql, KeyInputParam);
            if (int.Parse(dt.Rows[0][0].ToString()) == 0)
                throw new Exception("该记录不存在！");
            string updatesql = "";
            for (int j = 0; j < InputParam.Count; j++)
            {
                updatesql += "," + ColumnLeftChar + InputParam[j].ParameterName + ColumnRightChar;
                InputParam[j].ParameterName = "P" + j.ToString();
                updatesql += "=" + aiface.ParamChar + InputParam[j].ParameterName;
            }
            sql = "UPDATE " + tablename + " SET " + updatesql.Substring(1) + sql_where;
            InputParam.AddRange(KeyInputParam);
            aiface.ExecUpdate(sql, InputParam);
        }

        /// <summary>
        /// 通用删除记录
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="KeyInputParams">主键列表</param>
        public void CommDelete(string tablename, object[] KeyInputParams)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string sql = "";
                for (int j = 0; j < KeyInputParams.Length; j++)
                {
                    DbParamCollection KeyInputParam = (DbParamCollection)KeyInputParams[j];
                    if (j == 0)
                    {
                        string sql_where = "";
                        for (int i = 0; i < KeyInputParam.Count; i++)
                        {
                            sql_where += " AND " + ColumnLeftChar + KeyInputParam[i].ParameterName + ColumnRightChar;
                            sql_where += "=" + aiface.ParamChar + KeyInputParam[i].ParameterName;
                        }
                        if (sql_where.Length > 0) sql_where = " WHERE " + sql_where.Substring(" AND ".Length);
                        sql = "DELETE FROM " + tablename + sql_where;
                    }
                    aiface.ExecUpdate(sql, KeyInputParam);
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 获取工作流配置定义信息
        /// </summary>
        /// <param name="workflow_id">工作流编号</param>
        /// <returns>工作流配置定义信息</returns>
        public DataSet GetWorkflowInfo(string workflow_id)
        {
            DataSet dset = new DataSet();
            DataTable dt = aiface.ExecSelect("SELECT * FROM TB_WORKFLOW WHERE WORKFLOW_ID=" + workflow_id);
            dt.TableName = "TB_WORKFLOW";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_WORKFLOW_STEP WHERE WORKFLOW_ID=" + workflow_id + " ORDER BY STEP_ID");
            dt.TableName = "TB_WORKFLOW_STEP";
            dset.Tables.Add(dt);
            dt = aiface.ExecSelect("SELECT * FROM TB_WORKFLOW_STEP_RULE WHERE WORKFLOW_ID=" + workflow_id + " ORDER BY STEP_ID,RULE_ID");
            dt.TableName = "TB_WORKFLOW_STEP_RULE";
            dset.Tables.Add(dt);
            return dset;
        }

        /// <summary>
        /// 获取批注
        /// </summary>
        /// <param name="function_id">功能号</param>
        /// <param name="row_id">行号</param>
        /// <returns>批注信息</returns>
        public DataTable GetPostil(string function_id, string row_id)
        {
            string sql = "SELECT * FROM TB_POSTIL WHERE FUNCTION_ID=" + aiface.ParamChar + "FUNCTION_ID AND ROW_ID=" + aiface.ParamChar + "ROW_ID";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("FUNCTION_ID", function_id, DbParamType.Int, false));
            sql_params.Add(new DbParam("ROW_ID", row_id, DbParamType.VarChar, false));
            return aiface.ExecSelect(sql, sql_params);
        }

        /// <summary>
        /// 获取批注标题列表
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>批注标题列表</returns>
        public DataTable GetPostilTitleList(string username)
        {
            string sql = "SELECT DISTINCT TITLE FROM TB_POSTIL_TITLE WHERE USERNAME IS NULL OR USERNAME=" + aiface.ParamChar + "USERNAME";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
            return aiface.ExecSelect(sql, sql_params);
        }

        /// <summary>
        /// 添加批注标题列表
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="username">用户名</param>
        public void AddPostilTitleList(string title, string username)
        {
            string sql = "INSERT INTO TB_POSTIL_TITLE(TITLE,USERNAME) VALUES(" + aiface.ParamChar + "TITLE," + aiface.ParamChar + "USERNAME)";
            DbParamCollection sql_params = new DbParamCollection();
            sql_params.Add(new DbParam("TITLE", title, DbParamType.VarChar, false));
            sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
            aiface.ExecUpdate(sql, sql_params);
        }

        /// <summary>
        /// 保存批注
        /// </summary>
        /// <param name="postil_id">批注号</param>
        /// <param name="function_id">功能号</param>
        /// <param name="row_id">行号</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        public void SavePostil(string postil_id, string function_id, string row_id, string title, string content)
        {
            string username = GetLoginName();
            string sql = "";
            DbParamCollection sql_params = new DbParamCollection();
            if (postil_id.Length == 0)//添加
            {
                sql = "PR_POSTIL_ADD";
                sql_params.Add(new DbParam("FUNCTION_ID", function_id, DbParamType.Int, false));
                sql_params.Add(new DbParam("ROW_ID", row_id, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("TITLE", title, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("CONTENT", content, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                aiface.ExecUpdate(sql, sql_params, true);
            }
            else if (title.Length == 0)
            {
                sql = "DELETE FROM TB_POSTIL WHERE POSTIL_ID=" + aiface.ParamChar + "POSTIL_ID";
                sql_params.Add(new DbParam("POSTIL_ID", postil_id, DbParamType.Int, false));
                aiface.ExecUpdate(sql, sql_params);
            }
            else
            {
                sql = "UPDATE TB_POSTIL SET TITLE=" + aiface.ParamChar + "TITLE,CONTENT=" + aiface.ParamChar + "CONTENT,USERNAME=" + aiface.ParamChar + "USERNAME,POSTIL_TIME=GETDATE() WHERE POSTIL_ID=" + aiface.ParamChar + "POSTIL_ID";
                sql_params.Add(new DbParam("TITLE", title, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("CONTENT", content, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("USERNAME", username, DbParamType.VarChar, false));
                sql_params.Add(new DbParam("POSTIL_ID", postil_id, DbParamType.Int, false));
                aiface.ExecUpdate(sql, sql_params);
            }
        }

        /// <summary>
        /// 保存角色菜单权限
        /// </summary>
        /// <param name="role_id">角色编号</param>
        /// <param name="dt">数据表</param>
        public void SaveRoleMenu(string role_id, DataTable dt)
        {
            try
            {
                bool isBeginTran = aiface.BeginTransaction();
                string sql = "DELETE FROM TB_ROLE_MENU WHERE ROLE_ID=" + aiface.ParamChar + "ROLE_ID";
                DbParamCollection sql_params = new DbParamCollection();
                sql_params.Add(new DbParam("ROLE_ID", role_id, DbParamType.Int, false));
                aiface.ExecUpdate(sql, sql_params);
                sql = "INSERT INTO TB_ROLE_MENU(ROLE_ID,MENU_LEVEL,OPERATE_RIGHT) VALUES(" + aiface.ParamChar + "ROLE_ID," + aiface.ParamChar + "MENU_LEVEL," + aiface.ParamChar + "OPERATE_RIGHT)";
                sql_params.Add(new DbParam("MENU_LEVEL", DbParamType.VarChar));
                sql_params.Add(new DbParam("OPERATE_RIGHT", DbParamType.Int));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!dt.Rows[i]["OPERATE_RIGHT"].Equals(DBNull.Value))
                    {
                        sql_params[1].Value = dt.Rows[i]["MENU_LEVEL"];
                        sql_params[2].Value = dt.Rows[i]["OPERATE_RIGHT"];
                        aiface.ExecUpdate(sql, sql_params);
                    }
                }
                if (isBeginTran) aiface.Commit();
            }
            catch
            {
                aiface.Rollback();
                throw;
            }
        }

        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务
        /// </summary>
        public override void Dispose()
        {
            aiface.Dispose();
        }
    }
}
